<?php

class SalesOrder extends Eloquent {
	 protected $table = 'sales_order';

	/**
	 * Condition: Unarchived Items.
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @param $query
	 * @return mixed
	 */
	public function scopeActive( $query ) {
		return $query->where($this->table . '.archived', 0);
	}

	/**
	 * Retrieves Order Products under Sales Order.
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @return mixed
	 */
	public function salesOrderProducts() {
		return $this->hasMany('SalesOrderProduct', 'SALES_ORDER_id', 'id');
	}
	
	/**
	 * Retrieves Order Products under ORDER id that is not NDEL and Extra Large Item.
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @return mixed
	 */
	public function validOrderProducts() {
		return $this->hasMany('OrderProduct', 'ORDER_id', 'ORDER_id')
			->where('archived', 0)
			->where('SIZE_id', '<>', Constant::PROD_NDEL)
			->whereHas('Product', function($prod) {
				$prod->where('supplier_delivery', 0);
			});
	}

	/**
	 * Relates to Order..
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @return mixed
	 */
	public function order() {
		return $this->belongsTo('Order', 'ORDER_id');
	}
	
	/**
	 * Relates to User Creator.
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @return mixed
	 */
	public function createdBy() {
		return $this->belongsTo('User', 'created_by');
	}
	
	/**
	 * Relates to User Modifier.
	 * @author AngelaMisa 03/28/2015 3:18AM
	 * @return mixed
	 */
	public function modifiedBy() {
		return $this->belongsTo('User', 'modified_by');
	}
}